<template>
  <el-dropdown @command="changeLanguage">
    <el-button type="primary">
      {{ language }}
      <el-icon class="el-icon--right" style="margin-left: 5px"><ArrowDown /></el-icon>
    </el-button>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="zh">{{ $t('language.zh') }}</el-dropdown-item>
        <el-dropdown-item command="en">{{ $t('language.en') }}</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from '@/store'
import { ArrowDown } from '@element-plus/icons-vue'
import { useI18n } from 'vue-i18n'
const router = useRouter()
const store = useStore()
const i18n = useI18n()

// 国际化数据
const language = ref<string>('')
i18n.locale.value = store.state.i18n.language
language.value = store.state.i18n.language === 'zh' ? i18n.t('language.zh') : i18n.t('language.en')

function changeLanguage(val: string) {
  i18n.locale.value = val
  language.value = val === 'zh' ? i18n.t('language.zh') : i18n.t('language.en')
  store.commit('i18n/SET_LANGUAGE', val)
  // router.go(0) // 如果路由表都是静态，可不要这句话
}
</script>
